#include <tsin/library/DynamicTimer.hpp>

#include <tsin/util/Logger.hpp>

#include <cstring>

using namespace tsin;

DynamicTimer::DynamicTimer()
        : tick(0), timeout(-1)
{
}

void DynamicTimer::init(int timeout, DynamicObject *obj)
{
        this->timeout = timeout;
        this->obj = obj;
        tick = 0;
}

void DynamicTimer::reinit()
{
        for (int i = 0; i < 2; i++)
                arguments[i]->reinit();

        float arg1 = arguments[0]->getInt();
        DynamicObject *arg2 = arguments[1];

        switch(nrArguments) {
        case 2:
                if (strcmp(desp, "NN") == 0)
                        init(arg1, arg2);
                break;
        }
}

DynamicEvent DynamicTimer::postTick(void *, void *)
{
        if (tick >= timeout)
                return EVENT_END;

        obj->postTick();
        tick ++;
        if (tick >= timeout)
                return EVENT_END;
        return EVENT_NONE;
}

char DynamicTimer::getDescriptor()
{
        return 'N';
}

int DynamicTimer::getInt()
{
        return obj->getInt();
}

float DynamicTimer::getFloat()
{
        return obj->getFloat();
}

extern "C" LIBRARY_DLLEXPORT DynamicObject *bindTimer()
{
        return new DynamicTimer;
}
